﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.NetworkInformation;

namespace EthernetTools.Utils
{
    class Network
    {
        /// <summary>
        /// FF:FF:FF:FF:FF:FF
        /// </summary>
        public static byte[] EthBroadCastMac = new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };

        private static Dictionary<string, byte[]> MacAdressesBytes = new Dictionary<string, byte[]>();

        private static Dictionary<string, string> MacAdressesString = new Dictionary<string, string>();

        public static byte[] GetMyMacBytes(string id)
        {
            if (!MacAdressesBytes.ContainsKey(id))
            {
                MacAdressesBytes.Add(id, ByteUtils.ParseBytes(GetMyMacString(id)));
            }
            return MacAdressesBytes[id];
        }

        public static string GetMyMacString(string id)
        {
            if (MacAdressesString.ContainsKey(id))
            {
                return MacAdressesString[id];
            }

            NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();

            foreach (NetworkInterface ni in interfaces)
            {
                if (id.Contains(ni.Id))
                {
                    MacAdressesString.Add(id, ni.GetPhysicalAddress().ToString());
                    return ni.GetPhysicalAddress().ToString();
                }
            }

            return "";
        }

        public static byte[] GetMyMacBytes()
        {
            return GetMyMacBytes(MainWindow.BindedAdapterId);
        }

        public static string GetMyMacString()
        {
            return GetMyMacString(MainWindow.BindedAdapterId);
        }

        public static void GlobalRefresh()
        {
            MacAdressesBytes.Clear();
            MacAdressesString.Clear();
        }

    }
}
